﻿#region << 版 本 注 释 >>
/*----------------------------------------------------------------
 * 版权所有 (c) 2022   保留所有权利。
 * CLR版本：4.0.30319.42000
 * 机器名称：DESKTOP-UL84BU5
 * 公司名称：
 * 命名空间：KingerRobot.WanliMDC.SpiService.Services.BaseBussiness
 * 唯一标识：43387630-05f7-4939-8243-7b2d2372d09c
 * 文件名：CuringImpl
 * 当前用户域：DESKTOP-UL84BU5
 * 
 * 创建者：zzl
 * 电子邮箱：1003590782@qq.com
 * 创建时间：2022/6/8 14:05:45
 * 版本：V1.0.0
 * 描述：
 *
 * ----------------------------------------------------------------
 * 修改人：
 * 时间：
 * 修改说明：
 *
 * 版本：V1.0.1
 *----------------------------------------------------------------*/
#endregion << 版 本 注 释 >>
using KingerRobot.MexicoSailunMDC.Dao;
using KingerRobot.MexicoSailunMDC.Spi;
using Microsoft.VisualBasic;
using System.Reflection.PortableExecutable;

namespace KingerRobot.MexicoSailunMDC.SpiService;


public class CuringImpl : ICuringService
{

    private readonly static ICuringCallMaterialService _CuringCallMaterialService = new CuringCallMaterialImpl();
    private readonly static IRcsEquipCuring _RcsEquipCuring = new BRcsEquipCuring();
    private readonly static IRcsEquip _RcsEquip = new BRcsEquip();
    private readonly static IWcsEquipLoc _WcsEquipLoc = new BWcsEquipLoc();
    private readonly static IProdProduct _ProdProduct = new BProdProduct();
    private readonly static ITask _Task = new BTask();
    private readonly static ITaskCmd _TaskCmd = new BTaskCmd();
    private readonly static IWmsOutOrderSummary _WmsOutOrderSummary = new BWmsOutOrderSummary();
    private readonly static IWmsOutOrderSummarySafe _WmsOutOrderSummarySafe = new BWmsOutOrderSummarySafe();
    private readonly static IWcsLocSku _WcsLocSku = new BWcsLocSku();
    private readonly static IProdMaterial _ProdMaterial = new BProdMaterial();
    private readonly static IMesCuringRequestMaterial _MesCuringRequestMaterial = new BMesCuringRequestMaterial();
    private readonly static IWmsSetting _WmsSetting = new BWmsSetting();
    private readonly static IWcsEquipLocHold _WcsEquipLocHold = new BWcsEquipLocHold();
    private readonly static IMesAsrsCuringNormFMes _MesAsrsCuringNormFMes = new BMesAsrsCuringNormFMes();
    private readonly static IWcsManualCuringRecord _WcsManualCuringRecord = new BWcsManualCuringRecord();

    private static ConcurrentDictionary<string, DateTime> directory = new();
    public void MesSyncRequestMaterial(List<SyncRequestMaterialBean> syncRequestMaterialBeans)
    {

        var materials = _ProdMaterial.GetMaterialCodeAndId();
        var curingList = _RcsEquipCuring.GetEquipCurings();
        var wcsEquipLocs = _WcsEquipLoc.GetEquipLocIdAndEquipId();

        foreach (var requestMaterial in syncRequestMaterialBeans)
        {
            var equipNo = requestMaterial.EquipNo;
            var equipModel = requestMaterial.EquipModel ?? 0;
            var materialCode = requestMaterial.MaterialCode;
            var maxCacheCount = requestMaterial.MaxCacheCount;
            var realCacheCount = requestMaterial.RealCacheCount;
            var curingTime = requestMaterial.CuringTime;
            var currentCuringTime = requestMaterial.CurrentCuringTime;
            var curingTotalTime = requestMaterial.CuringTotalTime;
            var greentyrenoMes = requestMaterial.GreentyrenoMes;
            var equipStatusHost = requestMaterial.EquipStatusHost;
            var equipStatusMes = requestMaterial.EquipStatusMes;
            var isEnableRequest = requestMaterial.IsEnableRequest;
            var hostWmsFlag = requestMaterial.HostWmsFlag;


            //var statusMes = requestMaterial.EquipStatusMes;
            //var statusHost = requestMaterial.EquipStatusHost;



            // 获取硫化机位置(新)

            // 1:(1号右模；2号左模)；2:(1号左模；2号右模)
            var curings = curingList.Where(t => t.CuringCode == equipNo).ToList();
            var CuringSign = curings.FirstOrDefault()?.CuringSign;
            var curingOne = curings.Where(t => t.CuringModeIndex == 1).FirstOrDefault()?.Id ?? 0;
            var curingTwo = curings.Where(t => t.CuringModeIndex == 2).FirstOrDefault()?.Id ?? 0;

            var modelRL = _RcsEquipCuring.GetCuringRLByMesModel(equipModel);
            var equipId = 0L;
            if (modelRL == ConstFields.CuringModeL)
            {
                // 左
                equipId = (CuringSign == 1 ? curingTwo : curingOne);
            }
            else
            {
                // 右
                equipId = (CuringSign == 1 ? curingOne : curingTwo);
            }

            var locId = wcsEquipLocs.Where(t => t.EquipId == equipId).FirstOrDefault()?.LocId ?? 0;




            // 获取硫化机位置
            //var (locId, equipId) = _RcsEquipCuring.GetCuringLocByMESRL(equipNo, equipModel);

            // 获取物料信息
            var mesId = requestMaterial.MaterialId;
            var material = materials.Where(t => t.MaterialCode == materialCode).FirstOrDefault();//  _ProdMaterial.GetMaterialByCode(materialCode);
            var materialId = material?.MaterialId;

            // 获取预硫化物料信息
            var greentyrenoMesMaterial = materials.Where(t => t.MaterialCode == greentyrenoMes).FirstOrDefault();//  _ProdMaterial.GetMaterialByCode(materialCode);
            var greentyrenoMesMaterialId = greentyrenoMesMaterial?.MaterialId;



            _MesCuringRequestMaterial.AddOrUpdateCuringRequestMaterial(new POMesCuringRequestMaterial
            {
                CuringLocId = locId,
                EquipNo = equipNo,
                EquipModel = equipModel,
                MesMaterialId = mesId,
                MaterialId = materialId,
                GreentyrenoMes = greentyrenoMes,
                GreentyrenoMesMaterialId = greentyrenoMesMaterialId,

                MaxCacheCount = maxCacheCount,
                RealCacheCount = realCacheCount,
                CuringTime = curingTime,
                CurrentCuringTime = currentCuringTime,
                CuringTotalTime = curingTotalTime,
                UpdateTime = DateTime.Now,
                EquipStatusHost = equipStatusHost,
                EquipStatusMes = equipStatusMes,
                IsEnableRequest = isEnableRequest,
                HostWmsFlag = hostWmsFlag,

            });

        }

    }
    public List<CuringCallNo> GetCuringByCuringIdsRefCuring(List<long> curingLocIds)
    {

        var equips = _WcsEquipLoc.GetEquipIdsByLocIds(curingLocIds);
        var equipIds = equips.Select(t => t.EquipId.GetValueOrDefault()).ToList();

        var result = _RcsEquipCuring.GetCuringByCuringIds(equipIds);
        if (result is null)
        {
            return null;
        }
        return result.Select(t => new CuringCallNo
        {
            EquipId = t.Id.GetValueOrDefault(),
            LocId = equips.Where(s => s.EquipId == t.Id).FirstOrDefault()?.LocId ?? 0,
            CallNo = t.CuringCallNo.GetValueOrDefault(),
            CallSign = t.IsCuringCall.GetValueOrDefault(),
        }).ToList();
    }
    public GetCuringByCuringIdsRefCuringResult RequestMaterial(List<RequestMaterialBean> materialBeans)
    {
        GetCuringByCuringIdsRefCuringResult result = new GetCuringByCuringIdsRefCuringResult();
        if (materialBeans is null || materialBeans.Count == 0)
        {
            result.curingList = new List<CuringCallMaterialResult>();
            result.msg = string.Empty;
            result.status = 0;
            return result;
        }

        var calls = new List<CuringCallMaterialParam>();
        foreach (var materialBean in materialBeans)
        {
            var number = materialBean.Angle;

            if (string.IsNullOrWhiteSpace(number))
            {
                // TODO (注释)RpcHelper， 迁移到 BRpc 中实现
                // MES 获取角度
                //var service = K8.RSF.Client.Create<IMiddleMesService>();
                //number = service.GetCuringRequestAngle(materialBean.CuringCode, materialBean.CuringMode);
                //service.SetCuringRequestAngle(materialBean.CuringCode, materialBean.CuringMode);
            }

            decimal.TryParse(number, out var angle);


            calls.Add(new()
            {
                EquipId = materialBean.CuringEquipId,
                LocId = materialBean.CuringLocId,
                MidId = materialBean.Id,
                MachineNo = materialBean.CuringCode,
                Serno = materialBean.Serno,
                Spec = materialBean.Spec,
                ModeRL = _RcsEquipCuring.GetCuringRLByMesModel(materialBean.CuringMode.GetValueOrDefault()),
                Angle = angle,
                InvQty = materialBean.InvQty,
                CallSign = materialBean.CallSign,
                CallNo = materialBean.CallNo,
                Remark = materialBean.Remark,
            });
        }


        var wr = _CuringCallMaterialService.CreateCallMaterialOrder(calls);
        result.curingList = wr.Item1;
        result.status = wr.Item3;
        result.msg = wr.Item2;
        return result;
    }

    public ResultMes RequestMaterialByBarcode(List<RequestMaterialByBarcodeBean> materialBeans)
    {
        var result = new List<CuringCallMaterialResult>();
        if (materialBeans is null || materialBeans.Count == 0)
        {
            WriteLog($"按条码请料数据不存在1", true);
            return new ResultMes("参数为空", -1);
        }

        //升序(值越小优先级越高)
        materialBeans = materialBeans.Where(t => t.DetailBeans.Count > 0).OrderBy(t => t.OrderLevel).ToList();
        if (!materialBeans.Any())
        {
            WriteLog($"按条码请料数据不存在2", true);
            return new ResultMes("未找到请料明细", -2);
        }

        // 根据条码集合获取绑定货位信息
        var productCodes = materialBeans.SelectMany(t => t.DetailBeans).Select(t => t.TireBarcode).Distinct().ToList();
        var (haveLocs, prodSkuProducts, wcsLocSkus, wcsEquipLocHolds, products) = _WcsLocSku.GetWcsLocSkuBeansByCodes(productCodes);
        if (haveLocs.Count != productCodes.Count)
        {
            var haveProductCodes = haveLocs.Select(t => t.ProdProduct.ProductCode).ToList();
            var noProductCodes = productCodes.Except(haveProductCodes).ToList();
            StringBuilder stringBuilder = new StringBuilder();

            foreach (var productCode in noProductCodes)
            {
                // 条码无库存
                var product = products.Where(t => t.ProductCode == productCode).FirstOrDefault();
                if (product is null)
                {
                    stringBuilder.Append($"{productCode}条码不存在 | ");
                    continue;
                }
                var skuProduct = prodSkuProducts.Where(t => t.ProductId == product.Id).FirstOrDefault();
                if (skuProduct is null)
                {
                    stringBuilder.Append($"{productCode}条码绑定关系不存在 | ");
                    continue;
                }
                var wcsLocSku = wcsLocSkus.Where(t => t.SkuId == skuProduct.Id).FirstOrDefault();
                if (wcsLocSku is null)
                {
                    stringBuilder.Append($"{productCode}条码没有库存 | ");
                    continue;
                }

                // 堆垛机是否异常
                var wcsEquipLocHold = wcsEquipLocHolds.Where(t => t.LocId == wcsLocSku.LocId).FirstOrDefault();
                if (wcsEquipLocHold is null)
                {
                    var equipLocHold = _WcsEquipLocHold.GetEquipLocHoldByLoc(wcsLocSku.LocId.GetValueOrDefault());
                    if (equipLocHold is null)
                    {
                        stringBuilder.Append($"{productCode}条码没有库存 | ");
                        continue;
                    }

                    var rcsEquip = _RcsEquip.GetRcsEquipById(equipLocHold.EquipId.GetValueOrDefault());
                    if (rcsEquip is not null)
                    {
                        stringBuilder.Append($"{productCode}条码所在{rcsEquip.EquipName} 异常 | ");
                        continue;
                    }
                }
                stringBuilder.Append($"{productCode}条码没有库存 | ");
            }

            return new ResultMes($"{stringBuilder}", -3);
        }

        StringBuilder builder = new StringBuilder();
        // 请料对象封装
        foreach (var requestMaterial in materialBeans)
        {
            var detailBeans = requestMaterial.DetailBeans.GroupBy(t => t.EquipNo).Select(t => new
            {
                EquipNo = t.Key,
                CallBeans = t.ToList(),
            }).ToList();
            // var equipNoCount = detailBeans.Select(t => t.EquipNo).Distinct().Count();

            //添加硫化机左右模
            foreach (var callBean in detailBeans)
            {
                var calls = new List<CuringCallMaterialParam>();
                foreach (var detailBean in callBean.CallBeans)
                {
                    //通过条码查找判断是否有库存 
                    var wcsLocSku = haveLocs.Where(t => t.ProdProduct.ProductCode == detailBean.TireBarcode).FirstOrDefault();
                    if (wcsLocSku is null)
                    {
                        // 获取硫化机位置
                        var (locId, equipId) = _RcsEquipCuring.GetCuringLocByMESRL(detailBean.EquipNo, detailBean.EquipModel);
                        _RcsEquipCuring.UpdateCuringCallRemark(equipId, $"按条码请料,订单明细[{detailBean.OrderDetailId}]中的胎胚条码[{detailBean.TireBarcode}]未绑定货位|{DateTime.Now}");

                        continue;
                    }

                    // 是否已经创建出库
                    var isCall = calls.Where(t => t.Serno == detailBean.TireBarcode).Any();
                    if (isCall)
                    {
                        var (locId, equipId) = _RcsEquipCuring.GetCuringLocByMESRL(detailBean.EquipNo, detailBean.EquipModel);
                        _RcsEquipCuring.UpdateCuringCallRemark(equipId, $"按条码请料,条码{detailBean.TireBarcode}已经创建请料单|{DateTime.Now}");

                        continue;
                    }

                    // 条码是否已经创建订单
                    var startLoc = wcsLocSku.WcsLocSku.LocId.GetValueOrDefault();
                    var summary = _WmsOutOrderSummary.GetOutOrderSummaryByStartLocId(startLoc);
                    if (summary is not null)
                    {
                        var (locId, equipId) = _RcsEquipCuring.GetCuringLocByMESRL(detailBean.EquipNo, detailBean.EquipModel);
                        _RcsEquipCuring.UpdateCuringCallRemark(equipId, $"按条码请料,条码{detailBean.TireBarcode}所在开始位置{startLoc}已经创建请料单|{DateTime.Now}");

                        continue;
                    }

                    // 请料对象
                    var pMaterialCode = wcsLocSku.ProdMaterial.MaterialCode;
                    var midId = detailBean.OrderDetailId;
                    var serno = detailBean.TireBarcode;
                    var machineNo = detailBean.EquipNo;
                    var orderLevel = requestMaterial.OrderLevel;
                    var modeRL = _RcsEquipCuring.GetCuringRLByMesModel(detailBean.EquipModel);
                    calls.Add(new()
                    {
                        MidId = midId,
                        MachineNo = machineNo,
                        Serno = serno,
                        Spec = pMaterialCode,
                        ModeRL = modeRL,
                        MachineModel = detailBean.EquipModel,
                        InvQty = 1,
                        OrderNo = requestMaterial.OrderNo,
                        OrderPriority = orderLevel,
                        StartLoc = startLoc
                    });
                }
                // 创建订单
                var bean = _CuringCallMaterialService.CreateCallMaterialOrder(calls, (int)EnumCodeOutOrderType.WarehouseProductCode);
                builder.Append(bean.Item2);
                result.AddRange(bean.Item1);

            }
        }
        return result.Count > 0 ? new ResultMes("请料成功", 1) : new ResultMes($"请料失败:{builder}", 0);
    }

    public GetCuringByCuringIdsRefCuringResult RequestMaterialByMaterial(List<RequestMaterialByMaterialBean> materialBeans)
    {


        var result = new GetCuringByCuringIdsRefCuringResult();
        result.status = 0;
        result.msg = "";
        result.curingList = new List<CuringCallMaterialResult>();
        if (materialBeans is null || materialBeans.Count == 0)
        {
            result.msg = $"按规格、数量请料数据不存在1";
            WriteLog(result.msg, true);
            return result;
        }

        //升序(值越小优先级越高)
        materialBeans = materialBeans.Where(t => t.DetailBeans.Count > 0).OrderBy(t => t.OrderLevel).ToList();
        if (!materialBeans.Any())
        {
            result.msg = $"按规格、数量请料数据不存在2";
            WriteLog(result.msg, true);
            return result;
        }

        foreach (var materialBean in materialBeans)
        {
            var detailBeans = materialBean.DetailBeans.GroupBy(t => t.EquipNo).Select(t => new
            {
                EquipNo = t.Key,
                CallBeans = t.ToList(),
            }).ToList();

            //添加硫化机左右模
            foreach (var callBean in detailBeans)
            {
                var calls = new List<CuringCallMaterialParam>();
                foreach (var detailBean in callBean.CallBeans)
                {
                    //增加判断 物料是否存在等
                    var material = _ProdMaterial.GetMaterialByMesId(detailBean.MaterialId);
                    if (material is null)
                    {
                        var (locId, equipId) = _RcsEquipCuring.GetCuringLocByMESRL(detailBean.EquipNo, detailBean.EquipModel);
                        _RcsEquipCuring.UpdateCuringCallRemark(equipId, $"按规格、数量请料{detailBean.MaterialId} 物料不存在|{DateTime.Now}");
                        continue;
                    }

                    // 通过物料id获取物料编号
                    var pMaterialCode = material.MaterialCode;
                    var midId = detailBean.OrderDetailId;
                    var serno = detailBean.OrderDetailId;
                    var machineNo = detailBean.EquipNo;
                    var orderLevel = materialBean.OrderLevel;
                    var modeRL = _RcsEquipCuring.GetCuringRLByMesModel(detailBean.EquipModel);
                    var invQty = detailBean.OutputCount;
                    calls.Add(new()
                    {
                        MidId = midId,
                        MachineNo = machineNo,
                        Serno = serno,
                        Spec = pMaterialCode,

                        ModeRL = modeRL,
                        MachineModel = detailBean.EquipModel,
                        InvQty = invQty,
                        OrderPriority = orderLevel,
                        OrderNo = materialBean.OrderNo,
                    });
                }

                // 创建订单
                var bean = _CuringCallMaterialService.CreateCallMaterialOrder(calls, (int)EnumCodeOutOrderType.WarehouseMaterial);
                result.status = bean.Item3;
                result.msg = bean.Item2;
                result.curingList.AddRange(bean.Item1);
                if (result.status != 1)
                {
                    break;
                }
            }
        }

        return result;
    }

    public List<CuringCallMaterialResult> RequestMaterialByMiddleTable(List<RequestMaterialBean> materialBeans)
    {

        var result = new List<CuringCallMaterialResult>();
        if (materialBeans is null || materialBeans.Count == 0)
        {
            WriteLog($"硫化机台状态请料数据不存在1", true);
            return result;
        }

        var isCallSign = _WmsSetting.GetWmsSettingByCode(ConstFields.CuringCallMaterial);
        int.TryParse(isCallSign?.Value, out var sign);
        if (sign == 0)
        {
            WriteLog($"中间表请料关闭", true);
            return result;
        }

 


        //通过spec集合获取物料信息
        var specs = materialBeans.Select(t => t.Spec).ToList();
        var prodMaterials = _ProdMaterial.GetMaterialBySpecs(specs);

        //添加硫化机左右模
        var calls = new List<CuringCallMaterialParam>();
        foreach (var materialBean in materialBeans)
        {
            var modelRL = materialBean.CuringMode.GetValueOrDefault();

            //判断物料是否存在
            var isExist = prodMaterials.Where(t => t.Spec == materialBean.Spec).Any();
            if (!isExist)
            {
                var (locId, equipId) = _RcsEquipCuring.GetCuringLocByMESRL(materialBean.CuringCode, modelRL);
                _RcsEquipCuring.UpdateCuringCallRemark(equipId, $"硫化机[{materialBean.CuringCode}]请料信息中的物料[{materialBean.Spec}]不存在|{DateTime.Now}");
                continue;
            }

            var pMaterialCode = materialBean.Spec;
            var midId = materialBean.Id;
            var serno = materialBean.Serno;
            var machineNo = materialBean.CuringCode;
            var remark = materialBean.Remark;
            var canQty = materialBean.CanQty;
            var modeRL = _RcsEquipCuring.GetCuringRLByMesModel(modelRL);
            var invQty = materialBean.InvQty;

            calls.Add(new()
            {
                MidId = midId,
                MachineNo = machineNo,
                Serno = serno,
                Spec = pMaterialCode,
                ModeRL = modeRL,
                MachineModel = modelRL,
                InvQty = invQty,
                CanQty = canQty,
                Remark = remark,
            });
        }
        result = _CuringCallMaterialService.CreateCallMaterialOrder(calls).Item1;
        return result;
    }

    public int AddCuringBean(CuringBean curingBean)
    {
        if (curingBean is null)
        {
            return 0;
        }
        try
        {


            // TODO (注释)RpcHelper， 迁移到 BRpc 中实现
            //var middleMesService = K8.RSF.Client.Create<IMiddleMesService>();
            //return middleMesService.AddOutWarehouseFinish(new()
            //{
            //    BarCode = curingBean.BarCode,
            //    CuringCode = curingBean.CuringCode,
            //    Mode = curingBean.Mode,
            //    CreateTime = DateTime.Now,
            //    Serno = curingBean.Serno,
            //});

        }
        catch (Exception ex)
        {
            WriteLog(ex.Message, true);
        }

        return 1;
    }

    public int AddOutWarehouse(CuringBean curingBean)
    {
        if (curingBean is null)
        {
            return 0;
        }
        try
        {

            // TODO (注释)RpcHelper， 迁移到 BRpc 中实现
            //var middleMesService = K8.RSF.Client.Create<IMiddleMesService>();

            //middleMesService.AddOutWarehouse(new()
            //{
            //    BarCode = curingBean.BarCode,
            //    CuringCode = curingBean.CuringCode,
            //    Mode = curingBean.Mode,
            //    CreateTime = DateTime.Now,
            //});

        }
        catch (Exception ex)
        {
            WriteLog(ex.Message);
        }

        return 1;
    }

    public CuringBean GetCuringMode(int taskNo)
    {
        try
        {
            var db = Db.Default;
            var task = _Task.GetTaskByTaskNo(taskNo);
            if (task is null)
            {
                return null;
            }

            // var taskCmd = _TaskCmd.GetTaskCmdByTaskNo(taskNo);

            //var equipId = _WcsEquipLoc.GetEquipIdByLocId(task.TargetLoc.GetValueOrDefault());

            // 获取设备是：左模、右模
            //var curingRL = _RcsEquipCuring.GetCuringRLByCuringId(equipId);

            // 获取设备是：左模、右模 上面两个方法合并一个查询 提高效率
            var curingRL = _WcsEquipLoc.GetCuringLRByLocId(task.TargetLoc.GetValueOrDefault());

            var skuId = task.SkuId.GetValueOrDefault();

            //好像用不上 先注释
            //var summaryId = task.SummaryId.GetValueOrDefault();

            var barCode = _ProdProduct.GetCodeBySkuId(skuId);

            // var summary = _WmsOutOrderSummary.GetOutOrderSummaryById(summaryId);

            return new()
            {
                // CuringCode = curingRL?.CuringCode,
                Mode = curingRL?.Mode,
                BarCode = barCode,
                // Spray = false, //taskCmd?.IsSpray > 0,
                // Serno = summary?.Serno,
            };
        }
        catch (Exception ex)
        {
            WriteLog(ex.Message, true);
        }

        return null;
    }

    public BarCodeLoc GetBarCodeByLoc(long targetLoc)
    {
        BarCodeLoc barCodeLoc = new();
        var locSku = _WcsLocSku.GetWcsLocSkuByLocId(targetLoc);
        if (locSku?.SkuId is not long skuId)
        {
            return barCodeLoc;
        }
        var barCode = _ProdProduct.GetCodeBySkuId(skuId);
        if (string.IsNullOrWhiteSpace(barCode))
        {
            return barCodeLoc;
        }

        // 删除绑定关系
        _WcsLocSku.DeleteWcsLocSkuByLoc(targetLoc);

        barCodeLoc.Num = 1;
        barCodeLoc.BarCode = barCode;
        return barCodeLoc;
    }

    public int UpdateMesCuringNorm(MesCuringNormBean curingNorm)
    {

        //foreach (var curingNorm in curingNorms)
        //{
        var greentyreno = curingNorm.Greentyreno;
        var producers = curingNorm.Producers ?? 0;
        var quantity = curingNorm.Quantity;
        var updatetime = curingNorm.Updatetime;


        var result = _MesAsrsCuringNormFMes.AddOrUpdateCuringNorm(new POMesAsrsCuringNormFMes
        {
            Greentyreno = greentyreno,
            Producers = producers,
            Updatetime = updatetime,
            Quantity = quantity,
            Status = (int)EnumCuringNorm.New,
        });

        //}
        return result;

    }
    public int DeleteMesCuringNormByStatus(int ststus)
    {
        var result = _MesAsrsCuringNormFMes.DeleteMesCuringNormByStatus((int)EnumCuringNorm.Old);
        return result;
    }
    public int UpdateMesCuringNormByStatus(int ststus)
    {
        var result = _MesAsrsCuringNormFMes.UpdateMesCuringNormByStatus((int)EnumCuringNorm.Old);
        return result;
    }

    public int UpdateOrderSummarySafe(long targetLoc, int safe)
    {
        var result = _WmsOutOrderSummary.UpdateOutOrderSumarySafe(targetLoc, safe);

        return result;
    }
    public int UpdateSafe(long targetLoc)
    {
        var result = _WmsOutOrderSummarySafe.UpdateOutOrderSumarySafeByLocId(targetLoc);
        return result;
    }
    public BarCodeSafe GetOrderSafe(long targetLoc)
    {
        BarCodeSafe barCodeSafe = new();
        var safe = _WmsOutOrderSummarySafe.GetOutOrderSumarySafeByLocId(targetLoc);
        if (safe is not null)
        {
            // _WmsOutOrderSummarySafe.UpdateOutOrderSumarySafeByLocId(targetLoc);
            barCodeSafe.Num = 1;
            barCodeSafe.Safe = safe.Safe.GetValueOrDefault();
            return barCodeSafe;
        }
        return barCodeSafe;
    }

    public int AddOrderSafe(long summaryId, long targetLoc, int safe)
    {
        var result = _WmsOutOrderSummarySafe.AddOutOrderSumarySafe(summaryId, targetLoc, safe);
        return result;
    }
    /// <summary>
    /// 新增人工放胎记录
    /// </summary>
    public int AddManualCuringRecordByProductCode(string curingModelCode, string productCode, DateTime? mesPutTime)
    {
        var result = _WcsManualCuringRecord.AddManualCuringRecordByProductCode(curingModelCode, productCode, mesPutTime);
        return result;
    }
    /// <summary>
    /// 获取按时间倒序第一条同步人工记录
    /// </summary>
    /// <returns></returns>
    public POWcsManualCuringRecord GetFirstManualCuringRecordDesc()
    {
        var result = _WcsManualCuringRecord.GetFirstManualCuringRecordDesc();
        return result;
    }


}
